var express = require('express');
var router = express.Router();
var query = require('../my_modules/sql');

String.prototype.format= function() {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g, function (s, i) {
        return args[i];
    });
}

router.post('/', function (req, res) {
    console.log("处理登陆请求。。。");
    var name = req.body.name;
    var pwd = req.body.password;
    var email = req.body.email;
    var sql = "insert into users(name, password, email) values('{0}','{1}','{2}')".format(name,pwd,email);
    console.log(sql);
    query(sql, function (qerr,result,fields) {
        if(qerr){
            res.json({msg:'插入失败',status:'fail'});
        }else{
            if(result.affectedRows > 0){
                res.json({msg:'注册成功',status:'ok'});
            }else{
                res.json({msg:'插入失败',status:'fail'});
            }
        }
    });
});

module.exports = router;